//
//  TableViewControl.m
//  iCodeMac
//
//  Created by Andrew Barry on 29/03/09.
//  Copyright 2009 Barry Computer Services. All rights reserved.
//

#import "TableViewControl.h"
#import "ControlsDataSource.h"
#import "JavascriptForm.h"

@implementation TableViewControl
-(id) valueForProperty: (NSString *) name {
	if ([name isEqualToString:@"columns"]) {
		return columns;
	}
	if ([name isEqualToString:@"visibleHeader"]) {
		return [NSNumber numberWithBool:visibleHeader];
	}
	if ([name isEqualToString:@"columnReordering"]) {
		return [NSNumber numberWithBool:columnReordering];
	}
	if ([name isEqualToString:@"columnResizing"]) {
		return [NSNumber numberWithBool:columnResizing];
	}
	if ([name isEqualToString:@"multipleSelection"]) {
		return [NSNumber numberWithBool:multipleSelection];
	}
	if ([name isEqualToString:@"emptySelection"]) {
		return [NSNumber numberWithBool:emptySelection];
	}
	if ([name isEqualToString:@"dataSource"]) {
		return dataSource;
	}
	if ([name isEqualToString:@"dataField"]) {
		return dataField;
	}
	
	return [super valueForProperty: name];
}

-(void) setValue: (id) val forProperty:(NSString*) name {
	if ([name isEqualToString:@"columns"]) {
		[columns autorelease];
		columns = [val retain];
	} else if ([name isEqualToString:@"visibleHeader"]) {
		visibleHeader = [val boolValue];
	} else if ([name isEqualToString:@"columnReordering"]) {
		columnReordering = [val boolValue];
	} else if ([name isEqualToString:@"columnResizing"]) {
		columnResizing = [val boolValue];
	} else if ([name isEqualToString:@"multipleSelection"]) {
		multipleSelection = [val boolValue];
	} else if ([name isEqualToString:@"emptySelection"]) {
		emptySelection = [val boolValue];
	} else if ([name isEqualToString:@"dataSource"]) {
		dataSource = val;
	} else if ([name isEqualToString:@"dataField"]) {
		[dataField autorelease];
		dataField = [val retain];
	} else {
		[super setValue: val forProperty: name];
	}
}

-(NSArray *) getEvents {
	return [NSArray arrayWithObjects:@"RowCount", @"Row", @"SelectionChanged", @"DoubleClick", nil];
}

-(void) saveFields: (NSMutableDictionary *) content {
	if (columns)
		[content setObject:columns forKey:@"columns"];

	[self saveField: content forKey: @"visibleHeader"];
	[self saveField: content forKey: @"columnReordering"];
	[self saveField: content forKey: @"columnResizing"];
	[self saveField: content forKey: @"multipleSelection"];
	[self saveField: content forKey: @"emptySelection"];
	
	if (dataSource != nil && dataSource != (id)[NSNull null]) {
		[content setObject: [dataSource controlName] forKey: @"dataSource"];
	}
	if (dataField)
		[self saveField: content forKey: @"dataField"];
	
	[super saveFields: content];
}

-(NSView *) load: (NSDictionary *) defn withFrame: (NSRect) frame {
	columns = [defn objectForKey:@"columns"];
	if (!columns)
		columns = @"";
	
	if ([defn objectForKey:@"visibleHeader"])
		visibleHeader = [[defn objectForKey:@"visibleHeader"] boolValue];
	else
		visibleHeader = true;
	columnReordering = [[defn objectForKey:@"columnReordering"] boolValue];
	columnResizing = [[defn objectForKey:@"columnResizing"] boolValue];
	multipleSelection = [[defn objectForKey:@"multipleSelection"] boolValue];
	emptySelection = [[defn objectForKey:@"emptySelection"] boolValue];
	
	dataSourceLoadName = [defn objectForKey:@"dataSource"];
	dataField = [defn objectForKey:@"dataField"];

	NSScrollView *btn = [[NSScrollView alloc] initWithFrame:frame];
	NSTableView *tbl = [[NSTableView alloc] init];
	
	NSArray *cols = [columns componentsSeparatedByString: @","];
	for (int idx = 0; idx < [cols count]; idx++) {
		NSString *name = [cols objectAtIndex:idx];
		NSTableColumn *col = [[NSTableColumn alloc] initWithIdentifier:[NSNumber numberWithInt:idx]];
		[[col headerCell] setStringValue:name];
		[col setEditable:NO];

		[tbl addTableColumn: col];
	}
	
	if (!visibleHeader) {
		[tbl setHeaderView:nil];
	}
	[btn setAutohidesScrollers:YES];
	[btn setAutohidesScrollers:NO];

	[btn setHasVerticalScroller:YES];
	[btn setDocumentView:tbl];
	[btn setBorderType:NSBezelBorder];
	[tbl sizeToFit];
	return btn;
}

-(void) postLoad {
	FormEditor *editor = [formDocument editor];
	if ([dataSourceLoadName length] > 0) {
		dataSource = [editor controlByName:dataSourceLoadName];
	}
	
	[super postLoad];
}

-(NSString *) propsNibName {
	return @"ListViewProps";
}

@end
